﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.ComponentModel;
using System.Windows.Shapes;
using System.Windows.Media;

using Global;
using Clipper.Model;
using Communication;
using Clipper.Model.EnumModel;

namespace Clipper.BLL.DetectChain
{
    /// <summary>
    /// 反应条传送系统
    /// </summary>
    public partial class Transportation
    {
        #region 变量
        private static ObservableCollection<Transportation> transportationList;
        private static bool IsAction = true;//是否执行了行动
        private static  BackgroundWorker mBackWorker = null;
        private static AutoResetEvent mPause = new AutoResetEvent(false);//暂停
        private static SubstrateInfo mSbustrateBll = new SubstrateInfo();//底物信息

        #endregion

        #region 属性
        static bool mIsPause;
        /// <summary>
        /// 获得或设置模块暂停状态
        /// </summary>
        public static bool IsPause
        {
            get
            {
                return mIsPause;
            }
        }
        /// <summary>
        /// 小车的行动列表
        /// </summary>
        public static ObservableCollection<Transportation> TransportationList
        {
            get
            {
                return transportationList;
            }
            set
            {
                transportationList = value;
            }
        }

        private ReactionStrip transportStrip;
        /// <summary>
        /// 转运的反应条
        /// </summary>
        public ReactionStrip TransportStrip
        {
            get
            {
                return transportStrip;
            }
            set
            {
                transportStrip = value;
            }
        }
        ApplianceComponent fromApplianceComponent;
        /// <summary>
        /// 出发地
        /// </summary>
        public ApplianceComponent FromApplianceComponent
        {
            get
            {
                return fromApplianceComponent;
            }
            set
            {
                fromApplianceComponent = value;
            }
        }
        ApplianceComponent toApplianceComponent;
        /// <summary>
        /// 目的地
        /// </summary>
        public ApplianceComponent ToApplianceComponent
        {
            get
            {
                return toApplianceComponent;
            }
            set
            {
                toApplianceComponent = value;
            }
        }

        #endregion

        #region 添加小车任务和启动
        /// <summary>
        /// 添加小车转运动作，出发子系统的状态要在小车行动结束后变为空闲
        /// </summary>
        /// <param name="from">出发子系统</param>
        /// <param name="to">目的子系统</param>
        public static void AddTransportation(ReactionStrip strip, ApplianceComponent from, ApplianceComponent to)
        {
            if (transportationList == null)
            {
                transportationList = new ObservableCollection<Transportation>();
                
            }
            transportationList.Add(new Transportation()
            {
                TransportStrip = strip,
                FromApplianceComponent = from,
                ToApplianceComponent = to
            });
            SwitchActionStart(true);
        }

        /// <summary>
        /// 小车开始执行转运
        /// </summary>
        public static void SwitchActionStart(bool start)
        {
                //if (mTimer == null)
                //{
                //    mTimer = new System.Timers.Timer();
                //    mTimer.AutoReset = false;
                //    mTimer.Elapsed += new System.Timers.ElapsedEventHandler((ss, ee) =>
                //    {
                //        mTimer_Elapsed();
                //        mTimer.Start();
                //    });
                //    mTimer.Interval = 100.0;
                //    mTimer.Enabled = true;
                //    mTimer.Start();
                //}
                //else
                //{
                //    mTimer.Enabled = start;
                //}
            if (mBackWorker == null)
            {
                mBackWorker = new BackgroundWorker();
                mBackWorker.DoWork += (bss, bee) =>
                {
                    while (true)
                    {
                        mTimer_Elapsed();
                        Thread.Sleep(100);
                    }
                };
                mBackWorker.RunWorkerAsync();
            }
        } 
        /// <summary>
        /// 轮询事件,寻找空闲任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void mTimer_Elapsed()
        {
            if (!Transportation.IsPause)//暂停
            {
                Monitor.Enter(TransportationList);
                Transportation transport = null;
                if (transportationList.Count > 0)
                {
                    transport = transportationList[0];
                    //if (transportationList.Count == 1)
                    //{
                    //    RunTransport(transport);
                    //    return;
                    //}
                    ////空闲了再执行下一步
                    if (transport.ToApplianceComponent.DetectionStatus == ControlStatusEnum.IDLE)
                    {
                        RunTransport(transport);
                    }
                    else
                    {

                        Transportation idleTransportation = null;

                        //寻找空闲的任务
                        if (Transportation.TransportationList.Count > 1)
                        {
                            //首先寻找来自加试剂1的
                            try
                            {
                                var list = from tt in TransportationList
                                           where tt.FromApplianceComponent.GetType().Name == "AddReagent1AndSample" &&
                                               tt.ToApplianceComponent.DetectionStatus == ControlStatusEnum.IDLE
                                           select tt;

                                idleTransportation = list.First<Transportation>();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("--------------" + ex.Message + "------------");
                            }
                            if (idleTransportation == null)
                            {
                                for (int i=0;i<Transportation.TransportationList.Count;i++)
                                {
                                    try
                                    {
                                        Transportation _transport = TransportationList[i];
                                        if (_transport != null)
                                        {
                                            if (_transport.ToApplianceComponent.DetectionStatus == ControlStatusEnum.IDLE)
                                            {
                                                idleTransportation = _transport;
                                                break;
                                            }
                                            //先去掉
                                            //else if (_transport.FromApplianceComponent.GetType().Name == "AddReagent2" &&
                                            //    _transport.ToApplianceComponent.DetectionStatus == ControlStatusEnum.FINISHED)
                                            //{
                                            //    idleTransportation = _transport;
                                            //    break;
                                            //}
                                        }
                                        else
                                        {
                                            throw new Exception("任务丢失");
                                        }
                                    }
                                    catch(Exception ex)
                                    {
                                        Console.WriteLine(ex.ToString());
                                    }
                                }
                                if (idleTransportation != null)
                                {
                                    RunTransport(idleTransportation);
                                }
                            }
                            else
                            {
                                RunTransport(idleTransportation);
                            }
                        }
                    }

                }
                Monitor.Exit(TransportationList);
            }
        }

        #endregion

        #region 执行小车任务
        /// <summary>
        /// 调动小车转运反应条，并执行列表中的行动
        /// </summary>
        /// <param name="transport"></param>
        static void RunTransport(Transportation transport)
        {
            transport.ToApplianceComponent.DetectionStatus = ControlStatusEnum.BUSY;
            TransportCommand(transport);

        }

        #region 执行小车命令
        /// <summary>
        /// 根据出发地和目的地，确定小车执行的具体动作
        /// </summary>
        /// <param name="transport">行动</param>
        static void TransportCommand(Transportation transport)
        {
            IsAction = true;//默认为执行了
            if (Application.Current != null)
            {
                Console.WriteLine(string.Format("From:{0},To{1}", transport.FromApplianceComponent.GetType().Name, transport.ToApplianceComponent.GetType().Name));
                string fromComponentName = transport.FromApplianceComponent.GetType().Name;
                string toComponentName = transport.ToApplianceComponent.GetType().Name;
                switch (toComponentName)
                {
                    case "AddReagent2":
                        #region 加试剂2
                        switch (fromComponentName)
                        {
                            case "AddReagent1AndSample"://一步法
                                if (!ServiceProvider.IsSimulate)
                                {
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                                    Transportation.TransportA.GoIncubation();
                                    Transportation.TransportA.WaitStatus(3);
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1_1());
                                    Transportation.TransportA.Home();
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                                    Transportation.TransportB.GoIncubation();
                                    Transportation.TransportA.WaitStatus(0);
                                    transport.FromApplianceComponent.DetectionStatus = ControlStatusEnum.IDLE;//出发子系统空闲 
                                    DetectChainHelper.UnPosition(transport.FromApplianceComponent.PositionCode);
                                    Transportation.TransportB.WaitStatus(3);
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
                                    
                                    Transportation.TransportB.Home();
                                    Transportation.TransportB.WaitStatus(0);
                                    RunNextComponent(transport);
                                    IsAction = false;//设置过一次空闲了
                                }
                                else
                                {
                                    MoveStrip(ServiceProvider.Reagent1Position, transport.TransportStrip.ReactripView, ServiceProvider.Reagent2Position);
                                }
                                ServiceProvider.Monitor.R1Position = null;
                                break;
                            case "WashingTank"://设置洗涤位空闲,两步法
                                if (transport.TransportStrip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK1)
                                {
                                    WashingTank.WashTank1Status = ControlStatusEnum.IDLE;
                                    if (!ServiceProvider.IsSimulate)
                                    {
                                        //MoveStripFromWashToAddReagent2(1);
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash(1));
                                        TransportD.WaitStatus(TransportD.GoIncubation());
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash_1(1));
                                        TransportD.Home();
                                        TransportD.WaitStatus(0);
                                        IncubationTransport.WaitStatus(IncubationTransport.R2_1());
                                        TransportB.WaitStatus(TransportB.GoIncubation());
                                        IncubationTransport.WaitStatus(IncubationTransport.R2());
                                        TransportB.WaitStatus(TransportB.Home());
                                        DetectChainHelper.UnPosition(73);
                                        RunNextComponent(transport);

                                    }
                                    else
                                    {
                                        MoveStrip(ServiceProvider.WashTank1Position, transport.TransportStrip.ReactripView, ServiceProvider.Reagent2Position);
                                    }
                                    ServiceProvider.Monitor.W1Position = null;
                                }
                                else if (transport.TransportStrip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK2)
                                {
                                    WashingTank.WashTank2Status = ControlStatusEnum.IDLE;

                                    if (!ServiceProvider.IsSimulate)
                                    {
                                        //MoveStripFromWashToAddReagent2(2);
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash(2));
                                        TransportE.WaitStatus(TransportE.GoIncubation());
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash_1(2));
                                        TransportE.Home();
                                        TransportE.WaitStatus(0);
                                        IncubationTransport.WaitStatus(IncubationTransport.R2_1());
                                        TransportB.WaitStatus(TransportB.GoIncubation());
                                        IncubationTransport.WaitStatus(IncubationTransport.R2());
                                        TransportB.WaitStatus(TransportB.Home());
                                        DetectChainHelper.UnPosition(74);
                                        RunNextComponent(transport);
                                    }
                                    else
                                    {
                                        MoveStrip(ServiceProvider.WashTank2Position, transport.TransportStrip.ReactripView, ServiceProvider.Reagent2Position);
                                    }
                                    ServiceProvider.Monitor.W2Position = null;
                                }
                                break;
                        }
                        break;
                        #endregion
                    case "IncubationBox":
                        #region 孵育
                        byte seatNumber = IncubationBox.SeatList[0];
                        transport.TransportStrip.IncubatorSeat = seatNumber;
                        DetectChainHelper.Position(seatNumber);
                        transport.TransportStrip.ReactionStripPoint = new Point(ServiceProvider.WaitReactionPosition.X + 65 + seatNumber * 20, ServiceProvider.IncubationBox.Y);
                        IncubationBox.SeatList.Remove(seatNumber);
                        switch (fromComponentName)
                        {
                            case "AddReagent2"://一步法
                                if (!ServiceProvider.IsSimulate)
                                {
                                    StopRock();
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
                                    Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                                    Transportation.TransportB.Home();
                                    Transportation.TransportB.WaitStatus(0);
                                    IncubationBox.BedControl.WaitStatus(0);
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(seatNumber));
                                    IncubationBox.BedControl.Rock();
                                    RunNextComponent(transport);
                                }
                                else
                                {
                                    MoveStrip(ServiceProvider.Reagent2Position, transport.TransportStrip.ReactripView, transport.TransportStrip.ReactionStripPoint);
                                }
                                ServiceProvider.Monitor.R2Position = null;
                                break;
                            case "AddReagent1AndSample"://两步法
                                if (!ServiceProvider.IsSimulate)
                                {
                                    StopRock();
                                    //MoveStripFromReagent1ToBed(seatNumber);
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                                    Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());
                                    Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1_1());
                                    Transportation.TransportA.Home();
                                    IncubationBox.BedControl.WaitStatus(0);
                                    Transportation.IncubationTransport.DropStrip(seatNumber);
                                    IncubationBox.BedControl.Rock();
                                    Transportation.TransportA.WaitStatus(0);
                                    transport.FromApplianceComponent.DetectionStatus = ControlStatusEnum.IDLE;//出发子系统空闲   
                                    DetectChainHelper.UnPosition(transport.FromApplianceComponent.PositionCode);
                                    Transportation.IncubationTransport.WaitStatus(10);
                                    RunNextComponent(transport);
                                    IsAction = false;//设置过一次
                                }
                                else
                                {
                                    MoveStrip(ServiceProvider.Reagent1Position, transport.TransportStrip.ReactripView, transport.TransportStrip.ReactionStripPoint);
                                }
                                ServiceProvider.Monitor.R1Position = null;
                                break;
                        }
                        if (Application.Current != null)
                        {
                            Application.Current.Dispatcher.Invoke(new Action(() =>
                            {
                                //更新状态
                                ServiceProvider.Monitor.IncubationBoxReactionStrips.Add(transport.TransportStrip);//添加到孵育仓
                                
                            }));
                        }
                        break;
                        #endregion
                    case "WashingTank":
                        #region 洗涤
                        switch (fromComponentName)
                        {
                            case "IncubationBox":

                                IncubationBox.SeatList.Add(transport.TransportStrip.IncubatorSeat);//空出位置
                                DetectChainHelper.UnPosition(transport.TransportStrip.IncubatorSeat);
                                IncubationBox.SeatList = new ObservableCollection<byte>(IncubationBox.SeatList.OrderByDescending(x => x));

                                if (WashingTank.WashTank1Status == ControlStatusEnum.IDLE)
                                {
                                    transport.TransportStrip.ReactionStripSeat = ReactionStripStageEnum.WASHINGTANK1;
                                    WashingTank.WashTank1Status = ControlStatusEnum.BUSY;
                                    if (!ServiceProvider.IsSimulate)
                                    {
                                        StopRock();
                                        IncubationBox.BedControl.WaitStatus(0);
                                        IncubationTransport.WaitStatus(IncubationTransport.GetStrip(transport.TransportStrip.IncubatorSeat));
                                        IncubationBox.BedControl.Rock();
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash_1(1));
                                        TransportD.WaitStatus(TransportD.GoIncubation());
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash(1));
                                        TransportD.WaitStatus(TransportD.WashPosition());
                                        RunNextComponent(transport);
                                    }
                                    else
                                    {
                                        MoveStrip(transport.TransportStrip.ReactionStripPoint, transport.TransportStrip.ReactripView, ServiceProvider.WashTank1Position);
                                    }
                                }
                                else if (WashingTank.WashTank2Status == ControlStatusEnum.IDLE)
                                {
                                    transport.TransportStrip.ReactionStripSeat = ReactionStripStageEnum.WASHINGTANK2;
                                    WashingTank.WashTank2Status = ControlStatusEnum.BUSY;

                                    if (!ServiceProvider.IsSimulate)
                                    {
                                        StopRock();
                                        IncubationBox.BedControl.WaitStatus(0);
                                        IncubationTransport.WaitStatus(IncubationTransport.GetStrip(transport.TransportStrip.IncubatorSeat));
                                        IncubationBox.BedControl.Rock();
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash_1(2));
                                        TransportE.WaitStatus(TransportE.GoIncubation());
                                        IncubationTransport.WaitStatus(IncubationTransport.Wash(2));
                                        TransportE.WaitStatus(TransportE.WashPosition());
                                        RunNextComponent(transport);
                                    }
                                    else
                                    {
                                        MoveStrip(transport.TransportStrip.ReactionStripPoint, transport.TransportStrip.ReactripView, ServiceProvider.WashTank2Position);
                                    }
                                }
                                else
                                {
                                    IsAction = false;
                                }

                                if (IsAction)
                                {
                                    Monitor.Enter(ServiceProvider.Monitor);//更新孵育仓的数量和状态
                                    if (Application.Current != null)
                                    {
                                        Application.Current.Dispatcher.Invoke(new Action(() =>
                                        {
                                            ServiceProvider.Monitor.IncubationBoxReactionStrips.Remove(transport.TransportStrip);//移出孵育仓

                                        }));
                                    }
                                    Monitor.Exit(ServiceProvider.Monitor);
                                }

                                break;
                        }
                        break;
                        #endregion
                    case "AddSubstrate":
                        #region 加底物
                        do
                        {
                            if (ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount < 100)
                            {
                                //错误
                                if (ServiceProvider.Monitor.SubstrateState != ComponentStateEnum.ERROR)
                                {
                                    DetectChainHelper.PostMessage(3002);
                                    ServiceProvider.Monitor.SubstrateState = ComponentStateEnum.ERROR;
                                    //IsPause = true;//暂停
                                    //mPause.WaitOne();
                                }
                            }
                            else
                            {
                                //警告
                                if (ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount < 1000 &&
                                    ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount > 100)
                                {
                                    if (ServiceProvider.Monitor.SubstrateState != ComponentStateEnum.ALERT)
                                    {
                                        DetectChainHelper.PostMessage(2002);
                                    }
                                    ServiceProvider.Monitor.SubstrateState = ComponentStateEnum.ALERT;
                                }
                                else
                                {
                                    //正常
                                    ServiceProvider.Monitor.SubstrateState = ComponentStateEnum.NORMAL;
                                }
                            }
                        } while (ServiceProvider.Monitor.SubstrateState == ComponentStateEnum.ERROR);
                        if (transport.TransportStrip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK1)
                        {
                            if (!ServiceProvider.IsSimulate)
                            {
                                //AddSubstrate.Pump.PumpCharge(transport.transportStrip.ReactionCupList.Count*3200);//吸底物
                                //MoveStripFromWashToAddSubstrate(1);
                                IncubationTransport.WaitStatus(IncubationTransport.Wash(1));
                                TransportD.WaitStatus(TransportD.GoIncubation());
                                IncubationTransport.WaitStatus(IncubationTransport.Wash_1(1));
                                TransportD.Home();
                                IncubationTransport.WaitStatus(IncubationTransport.Substrate_1());
                                //AddSubstrate.Pump.WaitStatus(4);//等待吸底物
                                TransportC.WaitStatus(TransportC.GoIncubation());
                                IncubationTransport.WaitStatus(IncubationTransport.Substrate());
                                TransportC.WaitStatus(TransportC.Home());
                                RunNextComponent(transport);
                                TransportD.WaitStatus(0);
                                WashingTank.WashTank1Status = ControlStatusEnum.IDLE;
                                DetectChainHelper.UnPosition(73);
                                //更新底物数据
                                ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount -= ServiceProvider.ConfigVM.SubstrateAmount;
                                mSbustrateBll.Update(ServiceProvider.ReagentKitVM.SubstrateInfo);
                            }
                            else
                            {
                                MoveStrip(ServiceProvider.WashTank1Position, transport.TransportStrip.ReactripView, ServiceProvider.SubstractPosition);
                            }
                            ServiceProvider.Monitor.W1Position = null;
                        }
                        else if (transport.TransportStrip.ReactionStripSeat == ReactionStripStageEnum.WASHINGTANK2)
                        {

                            if (!ServiceProvider.IsSimulate)
                            {
                                //AddSubstrate.Pump.PumpCharge(transport.transportStrip.ReactionCupList.Count * 3200);//吸底物
                                IncubationTransport.WaitStatus(IncubationTransport.Wash(2));
                                TransportE.WaitStatus(TransportE.GoIncubation());
                                IncubationTransport.WaitStatus(IncubationTransport.Wash_1(2));
                                TransportE.Home();
                                IncubationTransport.WaitStatus(IncubationTransport.Substrate_1());
                                //AddSubstrate.Pump.WaitStatus(4);//等待吸底物
                                TransportC.WaitStatus(TransportC.GoIncubation());
                                IncubationTransport.WaitStatus(IncubationTransport.Substrate());
                                TransportC.WaitStatus(TransportC.Home());
                                RunNextComponent(transport);
                                TransportE.WaitStatus(0);
                                WashingTank.WashTank2Status = ControlStatusEnum.IDLE;
                                DetectChainHelper.UnPosition(74);
                                //更新底物数据
                                ServiceProvider.ReagentKitVM.SubstrateInfo.substrateAmount -= ServiceProvider.ConfigVM.SubstrateAmount;
                                mSbustrateBll.Update(ServiceProvider.ReagentKitVM.SubstrateInfo);
                            }
                            else
                            {
                                MoveStrip(ServiceProvider.WashTank2Position, transport.TransportStrip.ReactripView, ServiceProvider.SubstractPosition);
                            }
                            ServiceProvider.Monitor.W2Position = null;
                        }
                        break;
                        #endregion
                    case "WaitReaction":
                        #region 恒温等待

                        byte waitNumber = WaitReaction.SeatList[0];
                        transport.TransportStrip.WaitSeat = waitNumber;
                        DetectChainHelper.Position(waitNumber);
                        WaitReaction.SeatList.Remove(waitNumber);
                      
                        if (!ServiceProvider.IsSimulate)
                        {
                            StopRock();
                            //MoveStripFromAddSubstrateToWaiting(waitNumber);
                            IncubationTransport.WaitStatus(IncubationTransport.Substrate());
                            TransportC.WaitStatus(TransportC.GoIncubation());
                            IncubationTransport.WaitStatus(IncubationTransport.Substrate_1());
                            TransportC.Home();
                            TransportC.WaitStatus(0);
                            IncubationBox.BedControl.WaitStatus(0);
                            IncubationTransport.WaitStatus(IncubationTransport.DropStrip(waitNumber));
                            IncubationBox.BedControl.Rock();
                            RunNextComponent(transport);

                        }
                        else
                        {
                            transport.TransportStrip.ReactionStripPoint = new Point(ServiceProvider.WaitReactionPosition.X + waitNumber * 20, ServiceProvider.WaitReactionPosition.Y);
                            MoveStrip(ServiceProvider.SubstractPosition, transport.TransportStrip.ReactripView, transport.TransportStrip.ReactionStripPoint);
                        }
                        ServiceProvider.Monitor.SubPosition = null;
                        break;
                        #endregion
                    case "DetectionHouse":
                        #region 检测
                        WaitReaction.SeatList.Add(transport.TransportStrip.WaitSeat);//空出位置,暂时不空出位置，记得恢复
                        DetectChainHelper.UnPosition(transport.TransportStrip.WaitSeat);
                        WaitReaction.SeatList = new ObservableCollection<byte>(WaitReaction.SeatList.OrderByDescending(x => x));
                        Monitor.Enter(ServiceProvider.Monitor);//更新等待仓的数量和状态
                        if (Application.Current != null)
                        {
                            Application.Current.Dispatcher.Invoke(new Action(() =>
                            {
                                ServiceProvider.Monitor.WaitReactionStrips.Remove(transport.TransportStrip);//移出等待仓

                            }));
                        }
                        Monitor.Exit(ServiceProvider.Monitor);

                        if (!ServiceProvider.IsSimulate)
                        {
                            //MoveStripFromWaitingToDetecting(transport.TransportStrip.WaitSeat);
                            
                            StopRock();
                            IncubationBox.BedControl.WaitStatus(0);
                            IncubationTransport.WaitStatus(IncubationTransport.GetStrip(transport.TransportStrip.WaitSeat));
                            IncubationBox.BedControl.Rock();
                            IncubationTransport.WaitStatus(IncubationTransport.Detect_1());
                            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
                            IncubationTransport.WaitStatus(IncubationTransport.Detect());
                            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.PositionStrip());
                            RunNextComponent(transport);
                        }
                        else
                        {
                            MoveStrip(transport.TransportStrip.ReactionStripPoint, transport.TransportStrip.ReactripView, ServiceProvider.DetectionHoousePosition);
                        }

                         //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                         //           Transportation.TransportA.GoIncubation();
                         //           Transportation.TransportA.WaitStatus(3);
                         //           Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1_1());
                         //           Transportation.TransportA.Home();
                         //           Transportation.TransportA.WaitStatus(0);
                         //           transport.FromApplianceComponent.DetectionStatus = ControlStatusEnum.IDLE;//出发子系统空闲 
                         //           DetectChainHelper.UnPosition(transport.FromApplianceComponent.PositionCode);
                         //IncubationTransport.WaitStatus(IncubationTransport.Detect_1());
                         //   DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
                         //   IncubationTransport.WaitStatus(IncubationTransport.Detect());
                         //   DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.Position());
                         //   RunNextComponent(transport);
                         //   IsAction = false;
                        break;
                        #endregion
                    case "Reset":
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.IncubationReset());//复位
                        break;
                    case "RecycleBin"://调试用
                            IncubationTransport.WaitStatus(IncubationTransport.Detect());
                            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
                            IncubationTransport.WaitStatus(IncubationTransport.Detect_1());
                            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.ThrowStrip());
                            StopRock();
                            IncubationBox.BedControl.WaitStatus(0);
                            IncubationTransport.WaitStatus(IncubationTransport.DropStrip(transport.TransportStrip.WaitSeat));
                            IncubationBox.BedControl.Rock();
                            transport.fromApplianceComponent.DetectionStatus = ControlStatusEnum.IDLE;
                            DetectChainHelper.UnPosition(76);
                        break;
                }
                if (IsAction)
                {
                    transport.FromApplianceComponent.DetectionStatus = ControlStatusEnum.IDLE;//出发子系统空闲   
                    DetectChainHelper.UnPosition(transport.FromApplianceComponent.PositionCode);//清空位置记录
                }
            }
        }


        /// <summary>
        /// 小车运反应条动画,实际控制可按此方法,
        /// </summary>
        /// <param name="transport">运动</param>
        /// <param name="fromPosition">出发地</param>
        /// <param name="toPosition">目的地</param>
        public static void MoveStrip(Point fromPosition,FrameworkElement stripView,Point toPosition)
        {
            //小车现在位置
            Point tranNow = new Point(Canvas.GetLeft(ServiceProvider.Monitor.Transportation), Canvas.GetTop(ServiceProvider.Monitor.Transportation));
            Point tranFrom = new Point(fromPosition.X, tranNow.Y);//上车位置
            Point tranTo = new Point(toPosition.X, tranNow.Y);//下车位置
       

            //创建移动动画
            Storyboard storyboard = new Storyboard();

            //创建X轴方向动画，小车，假设每步要1s
            DoubleAnimationUsingKeyFrames _animationFrames = new DoubleAnimationUsingKeyFrames();
            _animationFrames.KeyFrames = new DoubleKeyFrameCollection(){
                new LinearDoubleKeyFrame()
                {
                     KeyTime=TimeSpan.FromSeconds(0),
                     Value=tranNow.X
                },
                new LinearDoubleKeyFrame()//小车就位1s
                {
                    KeyTime=TimeSpan.FromSeconds(1*ServiceProvider.ForwardTimes),
                    Value=tranFrom.X
                },
                new LinearDoubleKeyFrame()//等待上车
                {
                    KeyTime=TimeSpan.FromSeconds(2*ServiceProvider.ForwardTimes),
                    Value=tranFrom.X
                },
                new LinearDoubleKeyFrame()//下车
                {
                    KeyTime=TimeSpan.FromSeconds(3*ServiceProvider.ForwardTimes),
                    Value=tranTo.X
                }
            };

            Storyboard.SetTarget(_animationFrames, ServiceProvider.Monitor.Transportation);
            Storyboard.SetTargetProperty(_animationFrames, new PropertyPath("(Canvas.Left)"));
            storyboard.Children.Add(_animationFrames);

            //创建X轴方向动画，反应条
            DoubleAnimationUsingKeyFrames _animationFramesStrip = new DoubleAnimationUsingKeyFrames();
            _animationFramesStrip.KeyFrames = new DoubleKeyFrameCollection(){
                new LinearDoubleKeyFrame()//等车
                {
                     KeyTime=TimeSpan.FromSeconds(1*ServiceProvider.ForwardTimes),
                     Value=tranFrom.X
                },
                new LinearDoubleKeyFrame()
                {
                    KeyTime=TimeSpan.FromSeconds(2*ServiceProvider.ForwardTimes),
                    Value=tranFrom.X
                },
                new LinearDoubleKeyFrame()
                {
                    KeyTime=TimeSpan.FromSeconds(3*ServiceProvider.ForwardTimes),
                    Value=tranTo.X
                },
                new LinearDoubleKeyFrame()
                {
                    KeyTime=TimeSpan.FromSeconds(4*ServiceProvider.ForwardTimes),
                    Value=tranTo.X
                }
            };

            Storyboard.SetTarget(_animationFramesStrip, stripView);
            Storyboard.SetTargetProperty(_animationFramesStrip, new PropertyPath("(Canvas.Left)"));
            storyboard.Children.Add(_animationFramesStrip);

            //创建Y轴方向动画，反应条
            DoubleAnimationUsingKeyFrames _animationFramesStripY = new DoubleAnimationUsingKeyFrames();
            _animationFramesStripY.KeyFrames = new DoubleKeyFrameCollection(){
                new LinearDoubleKeyFrame()
                {
                     KeyTime=TimeSpan.FromSeconds(1*ServiceProvider.ForwardTimes),
                     Value=fromPosition.Y
                },
                new LinearDoubleKeyFrame()
                {
                    KeyTime=TimeSpan.FromSeconds(2*ServiceProvider.ForwardTimes),
                    Value=tranFrom.Y
                },
                new LinearDoubleKeyFrame()
                {
                    KeyTime=TimeSpan.FromSeconds(3*ServiceProvider.ForwardTimes),
                    Value=tranFrom.Y
                },
                new LinearDoubleKeyFrame()
                {
                    KeyTime=TimeSpan.FromSeconds(4*ServiceProvider.ForwardTimes),
                    Value=toPosition.Y
                }

            };
            Storyboard.SetTarget(_animationFramesStripY, stripView);
            Storyboard.SetTargetProperty(_animationFramesStripY, new PropertyPath("(Canvas.Top)"));
            storyboard.Children.Add(_animationFramesStripY);

            storyboard.Completed += (ss, ee) =>
                {
                    //mCompleteReset.Set();//完成信号
                };
            //动画播放
            storyboard.Begin();
        }

        #endregion 小车动画和命令

        #region 开始目的子系统工作
        /// <summary>
        /// 开始目的子系统的工作
        /// </summary>
        static void RunNextComponent(Transportation transport)
        {
            //if (IsAction)
            //{
            //    BackgroundWorker backWorker = new BackgroundWorker();
            //    backWorker.DoWork += (ss, ee) =>
            //        {
            //            transport.ToApplianceComponent.Run(transport.TransportStrip);
            //        };
            //    backWorker.RunWorkerAsync();
            //    transportationList.Remove(transport);//从列表中移除本次行动      
            //}
        }
        #endregion

        #endregion 执行小车任务

        #region 传送仓到试剂1、检测仓
        /// <summary>
        /// 反应条传送仓、检测仓动画和命令
        /// </summary>
        /// <param name="strip"></param>
        public static void TransportateNow(ReactionStrip strip, ApplianceComponent from, ApplianceComponent to)
        {
            to.DetectionStatus = ControlStatusEnum.BUSY;//目标位置忙碌
            //往下一步走
            Monitor.Enter(ServiceProvider.gReactionStripList);
            if (Application.Current != null)
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    if (ServiceProvider.IsSimulate)
                    {
                        if (strip.ReactripView == null)
                        {
                            Rectangle rect = new Rectangle()
                            {
                                Width = 9,
                                Height = 35,
                                Fill = Brushes.Red,

                            };
                            Label label = new Label()
                            {
                                Height = 35,
                                Content = strip.ID,
                                Background = Brushes.Red
                            };
                            strip.ReactripView = label;
                            label.Tag = strip;
                            ServiceProvider.Monitor.MonitorCanvas.Children.Add(label);
                        }
                    }

                    string toComponentName =to.GetType().Name;
                    switch (toComponentName)
                    {
                        case "AddReagent1AndSample":

                            if (!ServiceProvider.IsSimulate)
                            {
                            }
                            else
                            {
                                Move(ServiceProvider.DeliverPosition, strip.ReactripView, ServiceProvider.Reagent1Position).Begin();
                            }
                            break;
                        case "RecycleBin":
                            
                            if (!ServiceProvider.IsSimulate)
                            {
                                MoveStripFromDetectingToTrashCan();
                            }
                            else
                            {
                            Storyboard story= Move(ServiceProvider.DetectionHoousePosition, strip.ReactripView, ServiceProvider.RecyleBinPosition);
                            story.Completed+=(ss,ee)=>
                                {
                                    ServiceProvider.Monitor.MonitorCanvas.Children.Remove(strip.ReactripView);
                                    strip = null;
                                };
                            story.Begin();
                            }
                            break;

                    }
                }));
            }
            Monitor.Exit(ServiceProvider.gReactionStripList);

        }

        /// <summary>
        /// 直线动画
        /// </summary>
        /// <param name="fromPoint"></param>
        /// <param name="rect"></param>
        /// <param name="toPoint"></param>
        private static Storyboard Move(Point fromPoint, FrameworkElement rect, Point toPoint)
        {

            double fromX = Canvas.GetLeft(rect);
            double fromY = Canvas.GetTop(rect);
            
            //创建移动动画
            Storyboard storyboard = new Storyboard();
            //创建X轴方向动画
            DoubleAnimation doubleAnimation = new DoubleAnimation(
               fromPoint.X,
               toPoint.X,
               new Duration(TimeSpan.FromSeconds(1*ServiceProvider.ForwardTimes))
               );
            Storyboard.SetTarget(doubleAnimation, rect);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)"));
            storyboard.Children.Add(doubleAnimation);
            //创建Y轴方向动画
            doubleAnimation = new DoubleAnimation(
                fromPoint.Y,
                toPoint.Y,
                new Duration(TimeSpan.FromSeconds(1*ServiceProvider.ForwardTimes))
                );
            Storyboard.SetTarget(doubleAnimation, rect);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));
            storyboard.Children.Add(doubleAnimation);
            PointAnimationUsingKeyFrames dd = new PointAnimationUsingKeyFrames();
            return storyboard;
            //动画播放
            //storyboard.Begin();
        }

        #endregion


        #region // ---------反应条传送系统的分解动作---------------

        #region // 反应条出仓
        /// <summary>
        /// 反应条出仓
        /// </summary>
        public static string MoveOutStripFromCabin()
        {
            string cmd = "";
            cmd = "MV,G,2";
            Can.SendDataToCan(6, cmd);
            return cmd;
        }

        #endregion // 反应条出仓

        #region // 滑轨回零
        /// <summary>
        /// 滑轨回零
        /// </summary>
        /// <param name="mark"></param>
        public static void SlideHoming(Char mark)
        {
            // mark代表滑轨，其值分别是‘A’，‘B’，‘C’，‘D’，‘E’，‘F’，‘G’，‘H’
            string cmd = "";
            switch (mark)
            {
                case 'A':       // 加试剂1处滑轨
                    cmd = "MV,A,1";
                    Can.SendDataToCan(5, cmd);
                    break;

                case 'B':       // 加试剂2处滑轨
                    cmd = "MV,B,1";
                    Can.SendDataToCan(11, cmd);
                    break;

                case 'C':       // 加底物处滑轨
                    cmd = "MV,C,1";
                    Can.SendDataToCan(11, cmd);
                    break;

                case 'D':       // 洗板1处滑轨
                    cmd = "MV,D,1";
                    Can.SendDataToCan(12, cmd);
                    break;

                case 'E':       // 洗板2处滑轨
                    cmd = "MV,E,1";
                    Can.SendDataToCan(12, cmd);
                    break;

                case 'F':       // 检测处滑轨
                    cmd = "MV,F,1";
                    Can.SendDataToCan(15, cmd);
                    break;

                case 'G':       // 反应条仓滑轨
                    cmd = "MV,G,1";
                    Can.SendDataToCan(6, cmd);
                    break;

                case 'H':       // 孵育仓X方向滑轨
                    cmd = "MV,H,100";
                    Can.SendDataToCan(13, cmd);
                    break;

                default :
                    break;
            }
        }
        #endregion // 滑轨回零

        #region // 滑轨移到指定点（a,b,c,d,e,f）
        /// <summary>
        /// 滑轨移到指定点（a,b,c,d,e,f）
        /// </summary>
        /// <param name="mark"></param>
        public static void SlideMovingToAppointedSite(Char mark)
        {
            // mark代表滑轨，其值分别是‘A’，‘B’，‘C’，‘D’，‘E’，‘F’
            string cmd = "";

            switch (mark)
            {
                case 'A':
                    cmd = "MV,A,4";
                    Can.SendDataToCan(5, cmd);
                    break;

                case 'B':
                    cmd = "MV,B,4";
                    Can.SendDataToCan(11, cmd);
                    break;

                case 'C':
                    cmd = "MV,C,4";
                    Can.SendDataToCan(11, cmd);
                    break;

                case 'D':
                    cmd = "MV,D,3";
                    Can.SendDataToCan(12, cmd);
                    break;

                case 'E':
                    cmd = "MV,E,3";
                    Can.SendDataToCan(12, cmd);
                    break;

                case 'F':
                    cmd = "MV,F,4";
                    Can.SendDataToCan(15, cmd);
                    break;

                default:
                    break;
            }
        }
        #endregion // 滑轨移到指定点（a,b,c,d,e,f）

        #region // 滑轨移反应条到添加液，或洗板，或检测的位置
        /// <summary>
        /// 滑轨移反应条到添加液体，或洗板，或检测的位置
        /// </summary>
        /// <param name="mark"></param>
        public static void SlideMovingToFeatureSite(Char mark)
        {
            // mark代表滑轨，其值分别是‘A’，‘B’，‘C’，‘D’，‘E’，‘F’
            string cmd = "";
            short canid = 0;

            switch (mark)
            {
                case 'A':
                    canid = 5;
                    break;

                case 'B':
                    canid = 11;
                    break;

                case 'C':
                    canid = 11;
                    break;

                case 'D':
                    canid = 12;
                    break;

                case 'E':
                    canid = 12;
                    break;

                case 'F':
                    canid = 15;
                    break;

                default:
                    break;
            }
            cmd = "MV," + mark.ToString() + ",2";
            Can.SendDataToCan(canid, cmd);
        }
        #endregion // 滑轨移反应条到添加液体，或洗板，或检测的位置

        #region // 孵育仓小车（X轴）移到指定点
        /// <summary>
        /// 孵育仓小车（X轴）移到指定点
        /// </summary>
        /// <param name="site"></param>
        /// <param name="pos"></param>
        public static void TransporterMovingX(char site, char pos)
        {
            // site代表指定的点，其值分别是‘a’、‘b’、‘c’、‘d’、‘e’、‘f’
            // pos代表小车移到某点靠左，还是正对的位置，其值分别是‘L’、‘M’
            string cmd = "";
            int posID = 0;

            // 如果小车移到某点时，生成命令
            if (pos == 'M')
            {
                posID = Convert.ToByte(site) - 16;           // 81~86分别代表‘a’、‘b’、‘c’、‘d’、‘e’、‘f’
            }
            else if (pos == 'L')        // 如果小车需移到某点靠左的位置，则以当前位置为参考点，再向左移一定的距离
            {
                posID = Convert.ToByte(site) - 6;            // 91~96分别代表‘a’、‘b’、‘c’、‘d’、‘e’、‘f’的左位
            }

            cmd = "MV,H," + posID.ToString();
            Can.SendDataToCan(13, cmd);
        }
        #endregion // 孵育仓小车（X轴）移到指定的特殊点

        #region // 孵育仓小车（X轴）移到指定的孵育床位置
        /// <summary>
        /// 孵育仓小车（X轴）移到指定的孵育床位置
        /// </summary>
        /// <param name="pos"></param>
        //public static void TransporterMovingX(byte pos)
        //{
        //    // pos是孵育床位置编号1~70
          
        //    string cmd = "";
        //    cmd = "MV,H," + pos.ToString();
        //    Can.SendDataToCan(13, cmd);
        //}
        #endregion // 孵育仓小车（X轴）移到指定的孵育床位置             

        #endregion // ---------反应条传送系统的分解动作------------------

        #region // ----------反应条传送系统的组合动作--------------------

        #region // 从孵育床取反应条（GetStrip）
        /// <summary>
        /// 从孵育床取反应条
        /// </summary>
        public static void GetStripFromHatchBed()
        {
            string cmd = "GS";
            Can.SendDataToCan(13, cmd);
        }
        #endregion // 从孵育床取反应条（Get）

        #region // 放反应条到孵育床上（DropStrip）
        /// <summary>
        /// 放反应条到孵育床上
        /// </summary>
        public static void DropStripFromHatchBed()
        {
            string cmd = "DS";

            Can.SendDataToCan(13, cmd);
        }
        #endregion // 放反应条到孵育床上（Put）

        #region // 将反应条从反应条仓移到加试剂一的位置
        /// <summary>
        /// 将反应条从反应条仓移到加试剂一的位置
        /// </summary>
        public static void MoveStripFromCabinToReagent1()
        {
            //if (IsPause)
            //{
            //    mPause.WaitOne();
            //}

            //// 1. 反应条仓滑轨推反应条到滑轨A车上
            //if (ServiceProvider.gStatusSlideH == ControlStatusEnum.IDLE)
            //{
            //    MoveOutStripFromCabin();
            //    ServiceProvider.gStatusSlideH = ControlStatusEnum.BUSY;
            //}

            //// 循环等待，直到滑轨H状态为空闲
            //while (ServiceProvider.gStatusSlideH == ControlStatusEnum.BUSY)
            //{
            //    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            //}
            //if (IsPause)
            //{
            //    mPause.WaitOne();
            //}

            // 2. 滑轨A车推反应条到加试剂一位置 
            if (ServiceProvider.gStatusSlideA == ControlStatusEnum.IDLE)
            {
                SlideMovingToFeatureSite('A');
                ServiceProvider.gStatusSlideA = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideA == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            // 3. 反应条仓滑轨回零
        }
        #endregion // 将反应条从反应条仓移到加试剂一的位置

        #region // 将反应条从加试剂一移到加试剂二的位置
        /// <summary>
        /// 将反应条从加试剂一移到加试剂二的位置
        /// </summary>
        public static void MoveStripFromReagent1ToReagent2()
        {
            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 1. 孵育仓小车移到a点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('a', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }
            // 2. 滑轨A车推反应条到孵育仓小车上，即推到a点
            if (ServiceProvider.gStatusSlideA == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('A');
                ServiceProvider.gStatusSlideA = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideA == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 3. 孵育仓小车移到b点靠左的位置
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'L');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 4. 滑轨A车回到零位
            SlideHoming('A');

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 5. 滑轨B车移到b点
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 6. 孵育仓小车推反应条到b点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 7. 滑轨B车推反应条到加试剂二的等待位
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToFeatureSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从加试剂一移到加试剂二的位置

        #region // 将反应条从加试剂二移到孵育床某个位置
        /// <summary>
        /// 将反应条从加试剂二移到孵育床某个位置
        /// </summary>
        /// <param name="pos"></param>
        public static void MoveStripFromReagent2ToBed(byte pos)
        {
            // pos是孵育床的位置编号
            
            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 1. 孵育仓小车(X轴)移到b点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
            
            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 2. 滑轨B推反应条到b点
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 3. 孵育仓小车移到b点靠左的位置
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'L');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 4. 滑轨B回零
            SlideHoming('B');

            if (IsPause)
            {
                mPause.WaitOne();
            }


            // 6. 放反应条到孵育床上
            if (ServiceProvider.gStatusSlideKy == ControlStatusEnum.IDLE)
            {
                DropStripFromHatchBed();
                ServiceProvider.gStatusSlideKy = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKy == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从加试剂二移到孵育床的位置

        #region // 将反应条从孵育床移到洗板等待位
        /// <summary>
        /// 将反应条从孵育床移到洗板等待位
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="site"></param>
        public static void MoveStripFromBedToWash(byte pos, byte site)
        {
            // pos是孵育床位置编号, site为1代表第一个洗板位，为2代表第二个洗板位

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 2. 从孵育床取反应条
            if (ServiceProvider.gStatusSlideKy == ControlStatusEnum.IDLE)
            {
                GetStripFromHatchBed();
                ServiceProvider.gStatusSlideKy = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKy == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            // 3. 孵育仓小车(X轴)移到洗板位靠左的位置（另一种方案：X轴电机回零）
            // 4. 滑轨D或E移到d或e点
            // 5. 孵育仓小车(X轴)移到d点或e点
            // 6. 滑轨D或E移到洗板位            
            if (site == 1)      // 如果是洗板位1
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('D');
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    SlideMovingToFeatureSite('D');
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }
            else    // 如果是洗板位2
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }
                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('E');
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    SlideMovingToFeatureSite('E');
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }
        }
        #endregion // 将反应条从孵育床移到洗板等待位

        #region // 将反应条从洗板位移到加底物的位置
        /// <summary>
        /// 将反应条从洗板位移到加底物的位置
        /// </summary>
        /// <param name="pos"></param>
        public static void MoveStripFromWashToAddSubstrate(byte pos)
        {
            // pos为1代表第一个洗板位，为2代表第二个洗板位

            // 1. 孵育仓小车（X轴）移到d点或e点
            // 2. 洗板位滑轨D或E推反应条到d点或e点
            // 3. 孵育仓小车（X轴）推反应条到c点靠左的位置
            // 4. 滑轨D车或E车回零
            if (pos == 1)    // 如果是洗板位1
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('D');
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('c', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }
                SlideHoming('D');
            }
            else    // 如果是洗板位2
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('E');
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('c', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }
                SlideHoming('E');
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 5. 滑轨C车移到c点
            if (ServiceProvider.gStatusSlideC == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('C');
                ServiceProvider.gStatusSlideC = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideC == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 6. 孵育仓小车（X轴）推反应条到c点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('c', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 7. 滑轨C车推反应条到加底物的位置
            if (ServiceProvider.gStatusSlideC == ControlStatusEnum.IDLE)
            {
                SlideMovingToFeatureSite('C');
                ServiceProvider.gStatusSlideC = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideC== ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从洗板位移到加底物的位置

        #region // 将反应条从加底物到孵育床的恒温等待区
        /// <summary>
        /// 将反应条从加底物到孵育床的恒温等待区
        /// </summary>
        /// <param name="pos"></param>
        public static void MoveStripFromAddSubstrateToWaiting(byte pos)
        {
            // pos是恒温等待区的某个位置编号

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 1. 孵育仓小车（X轴）移到c点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('c', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 2. 滑轨C车推反应条到c点
            if (ServiceProvider.gStatusSlideC == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('C');
                ServiceProvider.gStatusSlideC = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideC == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 3. 孵育仓小车(X轴)回零   
            if (ServiceProvider.gStatusSlideH == ControlStatusEnum.BUSY)
            {
                SlideHoming('H');
                ServiceProvider.gStatusSlideH = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideH == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 4. 滑轨C回零
            if (ServiceProvider.gStatusSlideC == ControlStatusEnum.IDLE)
            {
                SlideHoming('C');
                ServiceProvider.gStatusSlideC = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideC == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }


            // 6. 放反应条到孵育床上
            if (ServiceProvider.gStatusSlideKy == ControlStatusEnum.IDLE)
            {
                DropStripFromHatchBed();
                ServiceProvider.gStatusSlideKy = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKy == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从加底物到孵育床的恒温等待区

        #region // 将反应条从恒温等待区移到检测位
        /// <summary>
        /// 将反应条从恒温等待区移到检测位
        /// </summary>
        /// <param name="pos"></param>
        public static void MoveStripFromWaitingToDetecting(byte pos)
        {
            // pos是恒温等待区的某个位置编号

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 2. 从孵育床取反应条
            if (ServiceProvider.gStatusSlideKy == ControlStatusEnum.IDLE)
            {
                GetStripFromHatchBed();
                ServiceProvider.gStatusSlideKy = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKy == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 3. 孵育仓小车(X轴)移到f点靠左的位置（另一种方案：X轴电机回零）
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('f', 'L');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 4. 滑轨F车移到都f点
            if (ServiceProvider.gStatusSlideF == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('F');
                ServiceProvider.gStatusSlideF = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideF == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 5. 孵育仓小车(X轴)移到f点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('f', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 6. 打开检测区进给阀门


            // 7. 滑轨F车移反应条到检测位       
            if (ServiceProvider.gStatusSlideF == ControlStatusEnum.IDLE)
            {
                SlideMovingToFeatureSite('F');
                ServiceProvider.gStatusSlideF = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideF == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从恒温等待区移到检测位

        #region // 将反应条从检测位移到废料仓
        /// <summary>
        /// 将反应条从检测位移到废料回收仓
        /// </summary>
        public static void MoveStripFromDetectingToTrashCan()
        {
            string cmd = "DG";
            Can.SendDataToCan(15, cmd);
        }
        #endregion // 将反应条从检测位移到废料回收仓

        #region // 将反应条从加试剂一移到孵育床的位置（两步法）
        /// <summary>
        /// 将反应条从加试剂一移到孵育床的位置（两步法）
        /// </summary>
        /// <param name="pos"></param>
        public static void MoveStripFromReagent1ToBed(byte pos)
        {
            // pos是孵育床的位置编号

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 1. 孵育仓小车(X轴)移到a点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('a', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 2. 滑轨A推反应条到a点
            if (ServiceProvider.gStatusSlideA == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('A');
                ServiceProvider.gStatusSlideA = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideA == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 3. 孵育仓小车(X轴)回零     
            if (ServiceProvider.gStatusSlideH == ControlStatusEnum.BUSY)
            {
                SlideHoming('H');
                ServiceProvider.gStatusSlideH = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideH == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 4. 滑轨A回零
            if (ServiceProvider.gStatusSlideA == ControlStatusEnum.IDLE)
            {
                SlideHoming('A');
                ServiceProvider.gStatusSlideA = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideA == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            ////// 5. 孵育仓小车(X轴)移反应条到指定的位置pos(根据实际情况定)
            ////if (ServiceProvider.gStatusSlideKx == ArmStatusEnum.IDLE)
            ////{
            ////    TransporterMovingX(pos);
            ////    ServiceProvider.gStatusSlideKx = ArmStatusEnum.BUSY;
            ////}

            ////while (ServiceProvider.gStatusSlideKx == ArmStatusEnum.BUSY)
            ////{
            ////    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            ////}

            ////if (IsPause)
            ////{
            ////    mPause.WaitOne();
            ////}

            // 6. 放反应条到孵育床上
            if (ServiceProvider.gStatusSlideKy == ControlStatusEnum.IDLE)
            {
                DropStripFromHatchBed();
                ServiceProvider.gStatusSlideKy = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKy == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从加试剂一孵育床的位置

        #region // 将反应条从洗板位到加试剂2等待位(两步法)
        /// <summary>
        /// 将反应条从洗板位到加试剂2等待位(两步法)
        /// </summary>
        /// <param name="site">洗涤位</param>
        public static void MoveStripFromWashToAddReagent2(byte site)
        {
            // 1. 孵育仓小车移到d点或e点
            // 2. 滑轨D或E将反应条移到d点或e点
            // 3. 孵育仓小车将反应条移到d点或e点的左位
            if (site == 1)      // 如果是洗板位1
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('D');
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }
            else    // 如果是洗板位2
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('E');
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }
            // 4. 滑轨D或E移到等待位 ?
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToFeatureSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 5. 孵育仓小车将反应条移到b点左位
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'L');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 6. 滑轨B移到b点
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 7. 孵育仓小车将反应条移到b点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 8. 滑轨B将反应条移到等待位 ?
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToFeatureSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }
        }
        #endregion // 将反应条从洗板位到加试剂2等待位

        #region // 将反应条从加试剂二移到洗板等待位（两步法）
        /// <summary>
        /// 将反应条从加试剂二移到加底物位置（两步法）
        /// </summary>
        /// <param name="pos"></param>
        public static void MoveStripFromReagent2ToWash(byte site)
        {
            // pos是孵育床的位置编号

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 1. 孵育仓小车(X轴)移到b点
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'M');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 2. 滑轨B推反应条到b点
            if (ServiceProvider.gStatusSlideB == ControlStatusEnum.IDLE)
            {
                SlideMovingToAppointedSite('B');
                ServiceProvider.gStatusSlideB = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideB == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

            if (IsPause)
            {
                mPause.WaitOne();
            }

            // 3. 小车推反应条到b点左位
            if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
            {
                TransporterMovingX('b', 'L');
                ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
            }

            while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
            {
                System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
            }

          
            // 4. 滑轨B移回等待位
           
            // 5. 孵育仓小车(X轴)移到d点或e点左位
            // 6. 滑轨D或E移到d点或e点         
            if (site == 1)      // 如果是洗板位1
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('D');
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('d', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideD == ControlStatusEnum.IDLE)
                {
                    SlideMovingToFeatureSite('D');  // ？到洗板等待位
                    ServiceProvider.gStatusSlideD = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideD == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }
            else    // 如果是洗板位2
            {
                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'L');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    SlideMovingToAppointedSite('E');
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideKx == ControlStatusEnum.IDLE)
                {
                    TransporterMovingX('e', 'M');
                    ServiceProvider.gStatusSlideKx = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideKx == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }

                if (IsPause)
                {
                    mPause.WaitOne();
                }

                if (ServiceProvider.gStatusSlideE == ControlStatusEnum.IDLE)
                {
                    SlideMovingToFeatureSite('E');  // ？到洗板等待位
                    ServiceProvider.gStatusSlideE = ControlStatusEnum.BUSY;
                }

                while (ServiceProvider.gStatusSlideE == ControlStatusEnum.BUSY)
                {
                    System.Threading.Thread.Sleep(ServiceProvider.gInteralmSec);
                }
            }           
        }
        #endregion // 将反应条从加试剂二移到加底物位置（两步法）

        #region  // 反应杯进给
        /// <summary>
        /// 反应杯进给
        /// </summary>
        /// <param name="site">代表位置，其值分别是‘A’，‘B’，‘C’，‘F’</param>
        /// <param name="pos">代表反应条的当前位置</param>
        public static void AssayCupFeeding(char site,byte pos)
        {
            byte sensorNo = 2;
            short canid = 0;

            // 设置传感器编号
            if (pos >= 4 && pos <= 8)
            {
                sensorNo = 3;
            }

            // 根据滑轨编号，取得其所属CanID
            switch (site)
            {
                case 'A':
                    canid = 5;
                    break;

                case 'B':
                    canid = 11;
                    break;

                case 'C':
                    canid = 11;
                    break;

                case 'F':
                    canid = 15;
                    break;
            }

            string cmd = "MV," + site.ToString() + "," + sensorNo.ToString();
            Can.SendDataToCan(canid, cmd);
        }
        #endregion  // 反应杯进给

        #endregion // ----------反应条传送系统的组合动作----------
    }
}
